package cclub.demo.controller.interview;

import cclub.demo.exception.SysException;
import cclub.demo.pojo.AO.interview.interviewProjectForm;
import cclub.demo.pojo.DO.interviewProjectDO;
import cclub.demo.response.ServiceResponse;
import cclub.demo.service.intf.interviewProjectService;
import cclub.demo.service.intf.interviewService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/interviewProject")
@Slf4j
public class InterviewProjectController {

    @NonNull
    private interviewProjectService interviewProjectService;
    @NonNull
    private interviewService interviewService;

    //新增面试项目
    @RequestMapping("/createInterviewProject")
    public Map createInterviewProject(@RequestParam("project_name") String project_name,
                                     @RequestParam("interview_num") Integer interview_num,
                                     @RequestParam("user_id")Integer userId){
        interviewProjectDO projectDO = new interviewProjectDO();
        projectDO.setUserId(userId);
        projectDO.setProjectName(project_name);
        projectDO.setInterviewNum(interview_num);
        System.out.println(projectDO);
        try {
            interviewProjectService.save(projectDO);
        }catch (Exception e){
            log.error("新增面试项目出现错误msg="+e.getMessage());
            throw new SysException();
        }
        return ServiceResponse.success();
    }

    //修改面试项目
    @RequestMapping("/modifyInterviewProject")
    public Map modifyInterviewProject(@RequestBody @Valid interviewProjectForm interviewProjectForm){
        // todo 后续针对一些流程做修改面试项目限制
        interviewProjectDO projectDO = new interviewProjectDO();
        projectDO.setId(interviewProjectForm.getId());
        projectDO.setProjectName(interviewProjectForm.getProjectName());
        projectDO.setInterviewNum(interviewProjectForm.getInterviewNum());
        try {
            interviewProjectService.updateById(projectDO);
        }catch (Exception e){
            log.error("修改面试项目出现错误异常msg={}",e.getMessage());
            throw new SysException();
        }
        return ServiceResponse.success();
    }

    //查询面试项目列表
    @RequestMapping("/queryInterviewProject")
    public Map queryInterviewProject(@RequestParam("user_id")Integer userId,
                                     @RequestParam("page") Integer page,
                                     @RequestParam("num") Integer num,
                                     @RequestParam("project_status") boolean  projectStatus){
        IPage<interviewProjectDO> pageList = interviewProjectService.queryInterviewProject(userId,page,num,projectStatus);
        return new HashMap<String,Object>(){{
            put("current",pageList.getCurrent());
            put("pages",pageList.getPages());
            put("size",pageList.getTotal());
            put("interviewList",pageList.getRecords());
        }};
    }

    //面试项目归档
    @RequestMapping("/placeOnFile")
    public Map placeOnFile(@RequestParam("id") Integer projectId){
        interviewProjectService.placeOnFile(projectId);
        return ServiceResponse.success();
    }

    //面试项目置顶
    @RequestMapping("/topping")
    public Map topping(@RequestParam("id") Integer projectId,
                       @RequestParam("topping_status") boolean toppingStatus){
        interviewProjectService.topping(projectId,toppingStatus);
        return ServiceResponse.success();
    }

    //删除面试项目接口
    @RequestMapping("/delInterviewProject")
    public Map delInterviewProject(@RequestParam("id") Integer projectId){
         interviewProjectService.removeInterviewProject(projectId);
         return ServiceResponse.success();
    }
}
